define(["jquery","store/account"], function($,$store){
	var defaults,timer,login = {};
	
	//显示异常信息提醒
	var showMessage = function(msg,time){
		time = time?time:5000;
		$("#"+defaults.domId+" .bb-message").text(msg);
		
		timer = setTimeout(() => {
			$("#"+defaults.domId+" .bb-message").text("");
		}, time);
	}
	
	//登录回调
	var callback = function(success,account,code,msg){
		if(success){
			sessionStorage.account = JSON.stringify(account);
			window.location.href = "home.html";
		}else{
			showMessage(msg);
		}
	}
	
	//登录提交事件
	var onSubmit = function(){
		var data = {};
		data.username = $("#"+defaults.domId+" .bb-username").val();
		data.password = $("#"+defaults.domId+" .bb-password").val();
		
		if(!data.username||data.username.trim().length!=11){
			showMessage("账号请输入11位手机号！");
			return;
		}
		
		if(!data.password||data.password.trim().length<6){
			showMessage("密码不能为空或者少于6位！");
			return;
		}
		
		$store.login(data,callback);
	}
	
	/**
	 * 初始化登录页面
	 * 入参：
	 * 	params.domId = 登录页面登录框DOM ID
	 * 登录框内元素Class命名规则：
	 * 	.bb-username 	= 账号
	 *  .bb-password 	= 密码
	 *  .bb-submit 	 	= 登录提交
	 *  .bb-reset 	 	= 重置表单
	 *  .bb-message  	= 错误提示信息
	 *  .bb-remember-me = 记住登录状态 
	 *  .bb-found-pwd	= 找回密码
	 */
	login.init = function(params){
		defaults = {
			domId:params.domId
		}
		$("#"+defaults.domId+" .bb-submit").click(onSubmit);
	}
	
	return login;
});